<template>
	<div id="subtmpl">
		<div class="inleft div" @click="substrict">-</div>
		<div class="incenter div" v-text="count"></div>
		<div class="inright div"  @click="add">+</div>
	</div>
</template>

<script>
    import {setItem,getItem,valueObj} from '../../kits/localSg.js'
	export default{
		props:['goodsid'],
		data(){
			return {
				count :1, //代表购买商品的数量
				resObj: {goodsid: 0, type: 'add'}
			}
		},
		created(){
			this.setcount();
		},
		methods:{
			setcount(){
				var obj = getItem();
				var that = this;
				obj.forEach(function(v,i){
					if(v.goodsid == that.goodsid){
						that.count = v.count;
					}
				});
				// console.log(this.goodsid);
			},
			substrict(){
				this.count--;
//				确保最小值为1
				if(this.count <1){
					this.count = 1;
				}
				this.sendmessage();
			},
			add(){
				this.count++;
				this.sendmessage();
			},
			sendmessage(){
				this.$emit('dataobj',this.count);
			}
		}
	}

</script>

<style scoped>
	#subtmpl .div{
		width: 40px;
		height: 25px;
		line-height: 25px;
		border: 1px solid #000;
		display: inline-block;
		text-align: center;
	}

</style>